<?php
include('../config.php');
include(PUNG_ROOT . '/admin/header.php');

$adminmenu = array(
LANG_011 => 'modules.php'
);

/*
 * Installing a module
 */

if(isset($_GET['install']))
{
	$include = $_GET['install'];
	
	include(PUNG_ROOT . '/modules/' . $include . '/info.php');
	
	if(!empty($install))
	{
		foreach($install as $query)
		{
			$db -> db_query($query);
		}
	}
	
	if(!empty($blocks))
	{
		foreach($blocks as $block)
		{
			
			$blockcontent = addslashes(get_contents(PUNG_ROOT . '/modules/' . $include . '/blocks/' . $block['content']));
			
			$db -> db_query("INSERT INTO " . DB_PREFIX . "blocks (title, content, type, system, position, visible, owner) VALUES (
			'" . $block['title'] . "',
			'" . $blockcontent . "',
			'" . $block['type'] . "',
			'" . $block['system'] . "',
			'" . $block['position'] . "',
			'" . $block['visible'] . "',
			'" . $include . "'
			)");
		}
	}

	$db -> db_query("INSERT INTO " . DB_PREFIX . "modules (date, folder, name, hasadmin, hasfront) VALUES (
	'" . date('Y-m-d') . "', 
	'" . $module['folder'] . "',
	'" . $module['name'] . "',
	'" . $module['hasadmin'] . "',
	'" . $module['hasfront'] . "'
	)");
	
	redirect_with_message('msg', 'LANG_076');
}

/*
 * Uninstalling a module
 */

if(isset($_GET['uninstall']))
{
	$include = $_GET['uninstall'];
	
	include(PUNG_ROOT . '/modules/' . $include . '/info.php');
	
	if(!empty($uninstall))
	{
		foreach($uninstall as $query)
		{
			$db -> db_query($query);
		}
	}
	
	if(!empty($blocks))
	{
		foreach($blocks as $block)
		{
			$db -> db_query("DELETE FROM " . DB_PREFIX . "blocks WHERE owner = '" . $include . "'");
		}
	}

	$db -> db_query("DELETE FROM " . DB_PREFIX . "modules WHERE folder = '" . $module['folder'] . "' LIMIT 1");
	
	redirect_with_message('msg', 'LANG_077');
}

$installed = $db -> db_query("SELECT * FROM " . DB_PREFIX . "modules ORDER BY date ASC");
$installed = $db -> db_result();

foreach($installed as $key)
{
	$array[] = $key['folder'];
}

$allmodules = scan_modules();

include(PUNG_ROOT . '/admin/head.php');
?>

<h3><?php print LANG_021; ?></h3>

<table width="100%">
<tr>
	<th><?php print LANG_022; ?></th>
	<th width="80"><?php print LANG_025; ?></th>
	<th width="80"><?php print LANG_023; ?></th>
	<th width="80"><?php print LANG_024; ?></th>
	<th width="80">&nbsp;</th>
</tr>
<?php
if(!empty($array))
{
foreach($allmodules as $single)
{
if(in_array($single, $array))
{
include(PUNG_ROOT . '/modules/' . $single . '/info.php');
?>
<tr>
	<td><?php print $module['name']; ?></td>
	<td><?php print $module['license']; ?></td>
	<td><?php print $module['version']; ?></td>
	<td><?php print $module['author']; ?></td>
	<td align="center"><a href="#" onclick="confirm_box('<?php print LANG_124; ?>', '?uninstall=<?php print $module['folder']; ?>');" 
	title="<?php print LANG_028; ?>"><img src="../images/admin_img/uninstall.png" alt="<?php print LANG_028; ?>" /></a></td>
</tr>
<?php
}
}
}
?>
</table>

<h3><?php print LANG_026; ?></h3>

<table width="100%">
<tr>
	<th><?php print LANG_022; ?></th>
	<th width="80"><?php print LANG_025; ?></th>
	<th width="80"><?php print LANG_023; ?></th>
	<th width="80"><?php print LANG_024; ?></th>
	<th width="80">&nbsp;</th>
</tr>
<?php
if(!empty($allmodules))
{
foreach($allmodules as $single)
{
if(!@in_array($single, $array))
{
include(PUNG_ROOT . '/modules/' . $single . '/info.php');
?>
<tr>
	<td><?php print $module['name']; ?></td>
	<td><?php print $module['license']; ?></td>
	<td><?php print $module['version']; ?></td>
	<td><?php print $module['author']; ?></td>
	<td align="center"><a href="#" onclick="confirm_box('<?php print LANG_125; ?>', '?install=<?php print $single; ?>');" 
	title="<?php print LANG_027; ?>"><img src="../images/admin_img/install.png" alt="<?php print LANG_027; ?>" /></a></td>
</tr>
<?php
}
}
}
?>
</table>

<?php
include(PUNG_ROOT . '/admin/footer.php');
?>